const App = {
    el: '#app',
    data: function () {
        return {
            menuData: menuData,
            menuDefaultProps: {
                children: 'childrens',
                label: 'name',
            },
            filterText: '',
            defaultExpandedKeys: ['root'],
            defaultCheckedKeys: ['root'],
            running: false,
            btn_test: '执行测试'
        };
    },
    methods: {
        exec() {
            console.log();
            let _this = this;
            if (_this.running) {
                alert("别猴急，正在拼命为你干着呢！");
                return;
            }
            _this.running = true;
            _this.btn_test = "执行测试中……";
            var concurrency = $("#concurrency").val();
            var number = $("#number").val();
            var basePath = $("#basePath").val();
            var mode = $("#mode").val();
            var env = $("#basePath").find("option:selected").attr("env").toLowerCase();
            var accessToken = $("#accessToken").val();
            var jwt =$("#jwt").val();
            testId = uuid()
            var reqParam = {
                "testId": testId,
                "accessToken": accessToken,
                "jwt":jwt,
                "basePath": basePath,
                "mode": mode,
                "env": env,
                "number": number,
                "concurrency": concurrency,
                "appName": appName,
                "apiIds": this.$refs.tree.getCheckedKeys()
            };

            $.ajax({
                type: "post",
                async: true,
                contentType: "application/json",
                dataType: 'json',
                data: JSON.stringify(reqParam),
                url: contextPath+"/test/pressure-test/exec?ajaxTime=" + new Date(),
                success: function (res) {
                    if(res.success==true){
                        getPressureTests();
                        alert("测试完成");
                    }else{
                        alert(res.message);
                    }
                    $("#progress").html("");
                    _this.btn_test = "执行测试";
                    _this.running = false;
                }
            });
            getProgress();
        }
    },
    watch: {},
    mounted() {
    }
};
const app = Vue.createApp(App);
app.use(ElementPlus);
app.mount("#app");


/**
 * 获取测试完成率
 * */
function getProgress() {
    $.ajax({
        type: "get",
        contentType: "json",
        url: contextPath+"/test/pressure-test/progress?testId=" + testId + "&ajaxTime=" + new Date(),
        success: function (res) {
            if (res.code == "0") {
                $("#progress").html("<font color='red'>就绪中</font>");
                setTimeout("getProgress()", 1000);
            } else if (res.code == "1") {
                $("#progress").html("已完成<font color='red'>" + res.completion + "%</font>");
                setTimeout("getProgress()", 1000);
            } else if (res.code == "2") {
                $("#progress").html("<font color='red'>测试完成,正在生成报告</font>");
                setTimeout("getProgress()", 1000);
            } else {
                $("#progress").html("");
            }
        }, err: function (e) {
            setTimeout("getProgress()", 3000);
        }
    });
}

function auth() {
    var env = $("#basePath").find("option:selected").attr("env").toLowerCase();
    var orgNo = $("#orgNo");
    var account = $("#account");
    var password = $("#password");
    if (orgNo.val() == "") {
        alert("组织编码不能为空！");
        orgNo.focus();
        return;
    }
    if (account.val() == "") {
        alert("账号不能为空！");
        account.focus();
        return;
    }
    if (password.val() == "") {
        alert("密码不能为空！");
        password.focus();
        return;
    }
    var reqParam = {
        "env": env,
        "orgNo": orgNo.val(),
        "account": account.val(),
        "password": password.val()
    };
    $.ajax({
        type: "get",
        contentType: "json",
        data: reqParam,
        url: contextPath+"/api/authorize?" + new Date(),
        success: function (res) {
            if (res.success) {
                $("#accessToken").val(res.data.accessToken);
                $("#jwt").val(res.data.jwt);
            } else {
                alert(res.message);
            }
        }
    });
}

/**
 * 获取压测列表
 * */
var pressureMap = {};

function getPressureTests() {
    $.ajax({
        type: "get",
        async: true,
        contentType: "application/x-www-form-urlencoded; charset=utf-8",
        data: {},
        url: contextPath+"/test/pressure-tests/"+appName+"?ajaxTime=" + new Date(),
        success: function (res) {
            var html = "";
            if (res.success == true) {
                for (i = 0; i < res.data.length; i++) {
                    var pressureTest = res.data[i];
                    pressureMap[pressureTest.id] = pressureTest;
                    var time = dateFormat(new Date(pressureTest.createDt));
                    html += "<tr id=\"tr-" + pressureTest.id + "\" odd=\"" + (i % 2 == 0) + "\">";
                    html += "<td style=\"text-align:left;\">";
                    html += "&#9672&nbsp;<a style=\"cursor:pointer;\" href='\/test\/report?testId=" + pressureTest.id + "' target=\"_blank\">" + pressureTest.name + "@" + time + "</a>";
                    html += "</td>";
                    html += "<td width=\"20\">";
                    html += "<a style=\"cursor:pointer;\" onclick=\"deletePressureTest('" + pressureTest.id + "')\">&#10005</a>";
                    html += "</td>";
                    html += "</tr>";
                }
            }
            $("#pressureTests").html(html);
        }
    });
}

/**
 * 删除
 * */
function deletePressureTest(id) {
    if (!confirm("你确定要删除吗？")) {
        return;
    }
    $.ajax({
        type: "delete",
        async: true,
        contentType: "application/x-www-form-urlencoded; charset=utf-8",
        data: {},
        url: contextPath+"/test/pressure-test/" + id + "?ajaxTime=" + new Date(),
        success: function (res) {
            if (res.success == true) {
                $("#tr-" + id).remove();
            }
        }
    });
}
